package com.luchao.back.controller;

import com.luchao.back.common.constant.Execute;
import com.luchao.back.common.constant.Validate;
import com.luchao.back.common.pojo.PageResult;
import com.luchao.back.common.pojo.ResponseResult;
import com.luchao.back.pojo.Sizes;
import com.luchao.back.service.SizesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "尺码管理")
@RestController
@RequestMapping("/sizes")
public class SizesController {

    @Autowired
    private SizesService sizesService;

    @ApiOperation("分页获取尺码列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "当前页长度", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "5")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getSizesByPage")
    public ResponseResult getSizesByPage(Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        long total = sizesService.getSizesCount();
        long totalPages = total % pageSize == 0 ? total / pageSize : (total / pageSize) + 1;
        pageNum = total <= pageSize ? 1 : pageNum;
        PageResult<Sizes> pageResult = new PageResult<Sizes>();
        pageResult.setPageNum(pageNum);
        pageResult.setCount(total);
        pageResult.setPage(totalPages);
        pageResult.setResult(sizesService.getSizesByPage(pageNum, pageSize));
        return ResponseResult.success(pageResult);
    }

    @ApiOperation("添加尺码")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sizes", value = "尺码对象信息", required = true, paramType = "formData", dataType = "Sizes", dataTypeClass = Sizes.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/addSizes")
    public ResponseResult addSizes(@RequestBody Sizes sizes) {
        return sizesService.addSizes(sizes) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "新增成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "新增失败", Execute.ERROR);
    }

    @ApiOperation("修改尺码信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sizes", value = "尺码对象信息", required = true, paramType = "formData", dataType = "Sizes", dataTypeClass = Sizes.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/editSizes")
    public ResponseResult editSizes(@RequestBody Sizes sizes) {
        return sizesService.editSizes(sizes) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "修改成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "修改失败", Execute.ERROR);
    }

    @ApiOperation("校验尺码唯一性")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sizenum", value = "尺码对象信息", required = true, paramType = "query", dataType = "float", dataTypeClass = float.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/checkSizes")
    public ResponseResult checkSizes(float sizenum) {
        return sizesService.checkSizes(sizenum) > 0 ? ResponseResult.success(ResponseResult.FAILED, "尺码已存在,不可以使用", Validate.ORDINARY) : ResponseResult.success(ResponseResult.SUCCESS, "尺码不存在,可以使用", Validate.UNIQUE);
    }

    @ApiOperation("批量禁用/解禁尺码")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "list", value = "尺码列表", required = true, paramType = "formData", dataType = "List<Sizes>", dataTypeClass = List.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/batchChangeState")
    public ResponseResult batchChangeState(@RequestBody List<Sizes> list) {
        return sizesService.batchChangeState(list) ? ResponseResult.success(ResponseResult.SUCCESS, "操作成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.ERROR, "操作失败", Execute.ERROR);
    }

    @ApiOperation("获取所有尺码")
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getAllSizes")
    public ResponseResult getAllSizes() {
        return ResponseResult.success(sizesService.getAllSizes());
    }

    @ApiOperation("获取所有前端展示的尺码")
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getAllSizesForView")
    public ResponseResult getAllSizesForView() {
        return ResponseResult.success(sizesService.getAllSizesForView());
    }
}
